package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class TodayBestService {
    @Autowired
    private UserService userService;

    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    @Value("${tanhua.sso.default.user}")
    private Long tanhuaSsoDefaultUser;

    @Autowired
    private UserInfoService userInfoService;



    public TodayBest queryTodayBest(String token) {
        //先解析token
        User user = userService.queryUserByToken(token);
        if (user == null) {
            return null;
        }

        //查询dubbo
        RecommendUser recommendUser = recommendUserApi.queryWithMaxScore(user.getId());

        TodayBest todayBest = new TodayBest();
        if (recommendUser == null) {
            todayBest.setId(tanhuaSsoDefaultUser);
            todayBest.setFateValue(80L);
        } else {
            todayBest.setId(recommendUser.getUserId());
            todayBest.setFateValue(recommendUser.getScore().longValue());
        }


        //查询mysql
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(todayBest.getId());

        if(null == userInfo){
            return null;
        }
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setAge(userInfo.getAge());

        return todayBest;
    }
}
